﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//建筑 / 装饰物
public class UnitBase : EntityBase
{
    public int gridX;
    public int gridY;
    public AssetReferenceSprite refSprite;
    private bool _isDraging;
    private SpriteRenderer _spriteRenderer;
    private int _oldGridX;
    private int _oldGridY;

    private WeakReference _unitMgrRef;
    private UnitMgr _unitMgr { get => _unitMgrRef.Target as UnitMgr; }
    public void Init(UnitMgr vUnitMgr)
    {
        _unitMgrRef = new WeakReference(vUnitMgr);
        SetParent(vUnitMgr.city.transform);
    }

    protected async override void Awake()
    {
        base.Awake();
        _spriteRenderer = this.GetComponentInChildren<SpriteRenderer>();
        _spriteRenderer.sprite = await refSprite.LoadAssetAsync<Sprite>().Task;
        _spriteRenderer.transform.localEulerAngles = new Vector3(90, 0, 0);
    }

    protected void SetParent(Transform vTransform)
    {
        transform.SetParent(vTransform, false);
    }

    public virtual void SetGrid(int vGridX, int vGridY)
    {
        gridX = vGridX;
        gridY = vGridY;

        float originX = GetOriginX();
        float originY = GetOriginY();

        //设置位置
        transform.localPosition = UtilGeometry.CreateVector3(
            originX + gridX * GetGridLenX(),
            originY + gridY * GetGridLenY()
            );
    }

    private float GetOriginX()
    {
        return _unitMgr.city.origin.x;
    }

    private float GetOriginY()
    {
        return _unitMgr.city.origin.PosY();
    }

    private float GetGridLenX()
    {
        return _unitMgr.city.gridLenX;
    }

    private float GetGridLenY()
    {
        return _unitMgr.city.gridLenY;
    }

    public void SetIsDraging(bool isDraging)
    {
        _isDraging = isDraging;
        if (_isDraging)
        {
            _spriteRenderer.color = Color.red;

            //记录旧的位置
            _oldGridX = gridX;
            _oldGridY = gridY;

            //播放可拖动效果
           StartCoroutine(DragEffect());

            //画出城区域
            CityLineGrid.Instance.SetVisible(true, _unitMgr.city);
        }
        else
        {
            _spriteRenderer.color = Color.white;

            //停止未播完的效果动画
            StopAllCoroutines();

            //恢复大小
            this.transform.localScale = Vector3.one;

            //检测位置
            if (transform.localPosition.x > GetOriginX() + _unitMgr.city.gridNumX * GetGridLenX() ||
                transform.localPosition.x < GetOriginX() ||
                transform.localPosition.PosY() > GetOriginY() + _unitMgr.city.gridNumY * GetGridLenY() ||
                transform.localPosition.PosY() < GetOriginY())
            {
                //如果在城外面，恢复位置
                SetGrid(_oldGridX, _oldGridY);
                Debug.Log("在城外:" + _oldGridX + " " + _oldGridY);
            }
            else
            {
                //如果在城内
                var vGridX = (int)((transform.localPosition.x - GetOriginX()) / GetGridLenX());
                var vGridY = (int)((transform.localPosition.PosY() - GetOriginY()) / GetGridLenY());
                SetGrid(vGridX, vGridY);
                Debug.Log("在城内:" + vGridX + " " + vGridY);
            }

            //隐藏城的区域
            CityLineGrid.Instance.SetVisible(false);
        }
    }

    //拖动效果
    private IEnumerator DragEffect()
    {
        float time = 0.3f;
        Vector3 vectorScale = Vector3.one * 3;
        for (float i = 0; i < time; i += Time.deltaTime)
        {
            this.transform.localScale = Vector3.Lerp(Vector3.one, vectorScale, i / time);
            yield return null;
        }
        var vectorScaleDrag = Vector3.one * 2;
        for (float i = 0; i < time; i += Time.deltaTime)
        {
            this.transform.localScale = Vector3.Lerp(vectorScale, vectorScaleDrag, i / time);
            yield return null;
        }
    }
}
